package com.site.service.impl;

import com.common.exception.BusinessException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.site.model.TrialApp;
import com.site.service.TrialAppService;
import com.site.mapper.TrialAppMapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by robin on 2016/12/14.
 * 试用申请ServiceImpl
 */
@Service
public class TrialAppServiceImpl implements TrialAppService{
    @Autowired
    private TrialAppMapper mapper;

    /**
     * 保存试用申请数据
     * @param app 试用申请对象
     * @return 结果
     */
    @Override
    public int save(TrialApp app) {
        if(StringUtils.isBlank(app.getAppName())) throw new BusinessException("请输入姓名");
        if(StringUtils.isBlank(app.getAppPhone())) throw new BusinessException("请输入手机号");
        app.setTime(new Date());
        return mapper.insertSelective(app);
    }

    /**
     * 修改使用申请备注
     * @param trialId 使用ID
     * @param remark  备注
     * @return 结果
     */
    @Override
    public int edit(String trialId, String remark) {
        TrialApp app = new TrialApp();
        app.setTrialId(trialId);
        app.setAppRemark(remark);
        return mapper.updateByPrimaryKeySelective(app);
    }

    /**
     * 查询申请数据
     * @param map      参数 开始日期：startDate 结束日期：endDate 关键字：key
     * @param pageNum  页码
     * @param pageSize 页面大小
     * @return 结果
     */
    @Override
    public PageInfo<TrialApp> search(Map map, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<TrialApp> list = mapper.search(map);
        return new PageInfo<TrialApp>(list);
    }

    /**
     * 根据Id查询诗句
     * @param trialId 试用Id
     * @return 结果
     */
    @Override
    public TrialApp selectById(String trialId) {
        return null;
    }
}
